package ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

import bussines.SubtitlesMatcher;
import bussines.logger.Log;
import bussines.logger.LogListener;
import bussines.progress.ProgressListener;
import bussines.progress.ProgressManager;

/**
 * 
 * @copyright (C) 2008 - Andrei Musca
 * @author andreimusca
 * @created : Oct 26, 2008
 * 
 **/
public class SubMacherFrame extends JFrame implements LogListener, ProgressListener {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JTextField jTextField = null;
	private JLabel jLabel = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextArea = null;
	private JPanel jPanel = null;
	private JSplitPane jSplitPane = null;
	private JPanel jPanel1 = null;
	private JProgressBar jProgressBar = null;
	private JFileChooser fc =null;

	/**
	 * This is the default constructor
	 */
	public SubMacherFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setSize(619, 332);
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Subtitle Matcher - Copyright (c) Andrei Musca 2008");
		this.setLocationRelativeTo(null);
		Log.addListener(this);
		ProgressManager.getInstance().addProgressListener(this);
		fc = new JFileChooser();
		fc.setDialogTitle("Choose a directory:");
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			jLabel = new JLabel();
			jLabel.setText("Choose folder:");
			jLabel.setName("jLabel");
			jContentPane = new JPanel();
			jContentPane.setLayout(gridLayout1);
			jContentPane.add(getJSplitPane(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Browse");
			jButton.setName("jButton");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (fc.showOpenDialog(SubMacherFrame.this) == JFileChooser.APPROVE_OPTION) {
						SubMacherFrame.this.getJTextField().setText(fc.getSelectedFile().getAbsolutePath());
						SubMacherFrame.this.getJButton1().setEnabled(true);
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Go");
			jButton1.setName("jButton1");
			jButton1.setEnabled(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jButton1.setEnabled(false);
					jTextArea.setText(null);
					new Thread(){
						@Override
						public void run() {
						    super.run();
						    new SubtitlesMatcher().match(jTextField.getText());
						}
					}.start();
					
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setName("jTextField");
			jTextField.setPreferredSize(new Dimension(354, 20));
			jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					File f = new File(jTextField.getText());
					getJButton1().setEnabled(f.exists() && f.isDirectory());
				}
			});
		}
		return jTextField;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(10);
			jPanel = new JPanel();
			jPanel.setLayout(flowLayout);
			jPanel.add(jLabel, null);
			jPanel.add(getJTextField(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			jSplitPane.setContinuousLayout(false);
			jSplitPane.setDividerSize(0);
			jSplitPane.setTopComponent(getJPanel());
			jSplitPane.setBottomComponent(getJPanel1());
		}
		return jSplitPane;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BoxLayout(getJPanel1(), BoxLayout.Y_AXIS));
			jPanel1.add(getJScrollPane(), null);
			jPanel1.add(getJProgressBar(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jProgressBar
	 * 
	 * @return javax.swing.JProgressBar
	 */
	private JProgressBar getJProgressBar() {
		if (jProgressBar == null) {
			jProgressBar = new JProgressBar();
			jProgressBar.setIndeterminate(false);
			jProgressBar.setStringPainted(true);
		}
		return jProgressBar;
	}

	public void onLog(String text) {
		getJTextArea().append(text + '\n');
	}

	public void onProgress(int step) {
		getJProgressBar().setValue(getJProgressBar().getValue() + 1);
	}

	public void onStart(int max) {
		getJProgressBar().setValue(0);
		getJProgressBar().setMaximum(max);
	}

} //  @jve:decl-index=0:visual-constraint="187,8"
